package com.github.shiqiyue.app.config.swagger;


import com.github.shiqiyue.app.common.result.ResultCode;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/***
 * swagger 配置类
 * 访问地址 swagger-doc.html
 * @author wwy
 *
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApi() {
        List<ResponseMessage> responseMessages = Lists.newArrayList();
        responseMessages.add(new ResponseMessageBuilder()
                .code(ResultCode.COMMON_SUCCESS).message("成功").build());
        responseMessages.add(new ResponseMessageBuilder()
                .code(ResultCode.COMMON_FAIL).message("一般错误").build());
        responseMessages.add(new ResponseMessageBuilder()
                .code(ResultCode.PERMISSION_DENY).message("权限不足").build());
        responseMessages.add(new ResponseMessageBuilder()
                .code(ResultCode.NOT_LOGIN).message("未登录").build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .ignoredParameterTypes(HttpSession.class, HttpServletRequest.class, HttpServletResponse.class, BindingResult.class).select()
                .apis(RequestHandlerSelectors.basePackage("com.github.shiqiyue.app.api.gapi"))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("标题")
                .description("描述")
                .version("版本").build();
    }

}
